package com.gonsin.ta.logic.parse.serialize;

import com.gonsin.ta.logic.enums.ConditionTypeEnum;
import com.gonsin.ta.logic.exceptions.TaException;
import com.gonsin.ta.logic.node.Node;
import com.gonsin.ta.logic.parse.condition.Executable;
import com.gonsin.ta.logic.parse.condition.SwitchCondition;
import com.gonsin.ta.logic.process.TaBus;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
@Slf4j
public class SwitchConditionSerial extends ConditionSerial{

    private Map<String, List<TaDeserializable>> switchMap;

    private ConditionTypeEnum conditionType = ConditionTypeEnum.SWITCH;

    @ApiModelProperty("选择节点名字")
    private String switchNode;

    @Override
    public Executable toExecutable(DeserializeContext deserializeContext) {

        Node node = TaBus.getNode(switchNode);
        if(node == null){
            throw new TaException("反序列化失败，找不到选择节点【{}】", switchNode);
        }

        SwitchCondition condition = new SwitchCondition(node);
        condition.setConditionId(this.getConditionId());
        switchMap.forEach((k, v) -> {
            List<Executable> executableList = new ArrayList<>();
            if(v == null){
                log.info("");
            }
            for(TaDeserializable exe : v){
                if(exe == null){
                    log.info("");
                }
                executableList.add(exe.toExecutable(deserializeContext));
            }
            condition.putCase(k, executableList);
        });
        return condition;
    }
}
